package com.dsg.workflow;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsg.entity.apply.DsgApplyBill;
import com.dsg.entity.approve.DsgApproveCase;
import com.dsg.service.apply.IDsgApplyCarService;
import com.dsg.service.apply.IDsgApplyCusService;
import com.dsg.service.apply.IDsgApplyPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * 轉換油咭（現有客戶->現有客戶）
 */
@Service
public class WorkflowService15 extends AbstractWorkflowService {

    static final Set<String> conditionalNodeSet = new HashSet<>();
    static {
        conditionalNodeSet.add(NODE_SUP_APPROVE);
    }

    @Autowired
    IDsgApplyCarService carService;
    @Autowired
    IDsgApplyCusService cusService;
    @Autowired
    IDsgApplyPayService payService;
    @Autowired
    WorkflowEngine engine;

    @Override
    protected String recalculateNodeName(DsgApplyBill bill) {
        return NODE_INPUT_CAR_INFO;
    }

    @Override
    protected Set getConditionalNodeSet(DsgApplyBill bill) {
        return conditionalNodeSet;
    }

    @Override
    protected boolean canGoOn(DsgApplyBill bill) throws Exception {
        boolean cusAudit = cusService.audit(bill);
        boolean payAudit = payService.payAuditAll(bill);
        return cusAudit && payAudit;
    }

    @Override
    protected boolean needNode(String currNodeName, DsgApplyBill bill, String nodeName) {
        switch (nodeName) {
            case NODE_DEDUCT_ONE:
                return !lastSuccess(bill);
        }
        return true;
    }

    @Override
    public void deductOneFillBack(DsgApproveCase approveCase) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("APP_NO", approveCase.getAppNo());
        DsgApplyBill bill = billService.getOne(wrapper);
        if (!lastSuccess(bill)) {
            tranValidate(approveCase.getAppNo());
        }
    }

    @Override
    public void approveFillBack(DsgApproveCase approveCase) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("APP_NO", approveCase.getAppNo());
        DsgApplyBill bill = billService.getOne(wrapper);
        if (lastSuccess(bill)) {
            tranValidate(approveCase.getAppNo());
        }
    }
}
